# Maintainer: Filipe Laíns (FFY00) <lains@archlinux.org>

pkgbase=ghdl
pkgname=('ghdl-mcode' 'ghdl-llvm' 'ghdl-gcc' 'python-pyghdl')
_gcc=10.2.0
_isl=0.23
pkgver=1.0.0
pkgrel=6
pkgdesc='VHDL simulator'
arch=('loong64' 'x86_64')
url='https://github.com/ghdl/ghdl'
license=('GPL2')
depends=('gcc-ada')
makedepends=('gmp' 'mpfr' 'libmpc' 'zlib' 'llvm' 'clang' 'python-setuptools')
validpgpkeys=('33C235A34C46AA3FFB293709A328C3A2C3C45C06'  # Jakub Jelinek <jakub@redhat.com>
              '13975A70E63C361C73AE69EF6EEB81F8981C74C7') # richard.guenther@gmail.com
source=("$pkgbase-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz"
        "$pkgbase-gcc11.patch::https://github.com/ghdl/ghdl/commit/8356ea3bb4e8d0e5ad8638c3d50914b64fc360ec.patch"
        "$pkgbase-llvm11.patch::https://github.com/ghdl/ghdl/commit/a45cd97fa291b442002d70ac7e0968a24b0815f9.patch"
        "$pkgbase-llvm12.patch::https://github.com/ghdl/ghdl/commit/2cfa1098070959413f5ed7c32220d9e55262e4e4.patch"
        "$pkgbase-llvm12-DILocation.patch::https://github.com/ghdl/ghdl/commit/396eb764f49666616063d4239df83b5aed961fff.patch"
        "$pkgbase-llvm13.patch::https://github.com/ghdl/ghdl/commit/e49cb7b91946bbe866e684c47ccb703a83f78dec.patch"
        "$pkgbase-llvm14.patch::https://github.com/ghdl/ghdl/commit/4775ebbd66a8108342ab3648025816ae1c419f38.patch"
        "https://ftp.gnu.org/gnu/gcc/gcc-$_gcc/gcc-$_gcc.tar.xz"{,.sig}
        "https://sources.easybuild.io/i/ISL/isl-$_isl.tar.bz2")
sha512sums=('efaa277132de3caeafedace137b58fa05bfa61567f12480092b1e8f74a4438e40c85c6e21cd0c5d65fbba3d116b65265e3939a8b5df951baf74837c1eca3196f'
            'aeef4fd149f6b58f7b458175833161cc07137cdbde07f2b5717d61f45997b6f6be37a2a59729204a50345e18f9ac6e28b69b60f01fbe01bea14fc1b114fbd8b5'
            '579d30687e44a02f9da5fca2096fcfd9767a4ce2b697fd96e207338a8bd6942e1173391417adcca4537951b4cbb09a8c9e91f97fc8ea181687009a86b99fb111'
            '8cc79c26aa1d301d9ee2ea58e9237b5a4e108de484f9df2a61df82501a59668eba4e6b7a47dde0717ba3f14a7cb6b8cbbd63e1a9d053d08fdd71a488f0a30fd6'
            '5e8c48ed15734d1cc2fb6e7be4a5480e56ba12e973a550ebf99183d44881325ca14e29a8f58367bd847a65a3a09e6b6e94d042fcfd77783e09539e5fb2ed4db0'
            'e9d6877c08f3100df4dac1455ba9c07b843e99c071a88e14711fed0ed459badf9f1e473ef9eb5ae674caba281039aa89882475ff4cb7fc7d09372f41004b2162'
            'a620eff0cae97fb5abae24b534098f79bd05ee8a5fed1c9ea893e05a4e1a1f68ff794aa9cd3936f539b1d875dd0e369857286e29b2606747a6fad7a136bf8e15'
            '42ae38928bd2e8183af445da34220964eb690b675b1892bbeb7cd5bb62be499011ec9a93397dba5e2fb681afadfc6f2767d03b9035b44ba9be807187ae6dc65e'
            'SKIP'
            '05dfbdbbe852e2440b272808b23210374ce4ce46298eeb4c18ca8725db834b0da607338a371e3d07a78585fc7e0de182e75bf09dff11477937228bd97b7b83f7')
options=(!lto)

prepare() {
  cd "$srcdir"

  cd $pkgbase-$pkgver
  patch -p1 -i ../$pkgbase-gcc11.patch
  patch -p1 -i ../$pkgbase-llvm11.patch
  patch -p1 -i ../$pkgbase-llvm12.patch
  patch -p1 -i ../$pkgbase-llvm12-DILocation.patch
  patch -p1 -i ../$pkgbase-llvm13.patch
  patch -p1 -i ../$pkgbase-llvm14.patch || :
  cd ..

  cp -r $pkgbase-$pkgver ghdl-mcode
  cp -r $pkgbase-$pkgver ghdl-llvm
  cp -r $pkgbase-$pkgver ghdl-gcc
  cp -r $pkgbase-$pkgver pyghdl

  mkdir gcc-build

  cd gcc-$_gcc

  ln -s ../isl-$_isl isl

  echo $_gcc > gcc/BASE-VER

  # hack! - some configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure
}

_configure() {
  ./configure \
      --prefix=/usr \
      --disable-werror \
      --enable-checks \
      --enable-libghdl \
      --enable-synth $@
}

build() {
  export GNATMAKE="gnatmake $MAKEFLAGS"

  echo 'Building ghdl-mcode...'
  cd "$srcdir"/ghdl-mcode

  _configure

  make

  echo 'Building ghdl-llvm...'
  cd "$srcdir"/ghdl-llvm

  _configure --with-llvm-config

  make

  echo 'Building ghdl-gcc...'
  cd "$srcdir"/ghdl-gcc

  _configure --with-gcc="$srcdir"/gcc-$_gcc

  make copy-sources

  cd "$srcdir"/gcc-build

# Taken from gcc PKGBUILD
  CFLAGS=${CFLAGS/-Werror=format-security/}
  CXXFLAGS=${CXXFLAGS/-Werror=format-security/}

  "$srcdir"/gcc-$_gcc/configure \
      --prefix=/usr \
      --libdir=/usr/lib \
      --libexecdir=/usr/lib \
      --enable-languages=vhdl \
      --enable-default-pie \
      --enable-default-ssp \
      --with-isl \
      --enable-plugin \
      --enable-lto \
      --disable-bootstrap \
      --disable-multilib \
      --disable-libada \
      --disable-libsanitizer \
      --disable-libssp \
      --disable-libquadmath \
      --disable-libgomp \
      --disable-libvtv \
      --with-pkgversion='Arch Linux Repositories' \
      --with-bugurl='https://bugs.archlinux.org/'

  make

  cd "$srcdir"/ghdl-gcc

  make \
      GHDL_GCC_BIN="$srcdir"/gcc-build/gcc/ghdl \
      GHDL1_GCC_BIN="--GHDL1=$srcdir/gcc-build/gcc/ghdl1" \
      ghdllib

  echo 'Building pyghdl...'
  cd "$srcdir"/pyghdl

  python setup.py build
}

#check() {
#  cd $pkgbase-$pkgver/testsuite
#
#  echo 'Running tests for ghdl-mcode...'
#  GHDL="$srcdir"/ghdl-mcode/ghdl_mcode ./testsuite.sh
#
#  echo 'Running tests for ghdl-llvm...'
#  GHDL="$srcdir"/ghdl-llvm/ghdl1-llvm ./testsuite.sh
#
#  echo 'Running tests for ghdl-gcc...'
#  GHDL="$srcdir"/gcc-build/gcc/ghdl ./testsuite.sh
#}

package_ghdl-mcode() {
  pkgdesc="$pkgdesc (mcode backend)"
  provides=('ghdl')
  conflicts=('ghdl')

  cd $pkgname

  make DESTDIR="$pkgdir" install
}

package_ghdl-llvm() {
  pkgdesc="$pkgdesc (LLVM backend)"
  depends+=('llvm-libs')
  provides=('ghdl')
  conflicts=('ghdl')
  options=(!emptydirs)

  cd $pkgname

  make DESTDIR="$pkgdir" install

  # strip binaries
  find "$pkgdir"/usr/bin/ -type f -and \( -executable -o -name '*.o' \) -exec strip '{}' \;
}

package_ghdl-gcc() {
  pkgdesc="$pkgdesc (GCC backend)"
  provides=('ghdl')
  conflicts=('ghdl')
  options=(!emptydirs)

  cd "$srcdir"/gcc-build

  make DESTDIR="$pkgdir" install

  # Remove files that conflict with the system gcc -- we only want to install the ghdl plugin
  for FILE in $(find "$pkgdir" -not -type d); do
    if [ -f /"${FILE#"$pkgdir"}" ]; then
      rm -f "$FILE"
    fi
  done

  find "$pkgdir"/usr/share -not -type d -not -name '*ghdl*' -delete
  rm -rf "$pkgdir"/usr/lib64

  # strip binaries
  find "$pkgdir"/usr/bin/ "$pkgdir"/usr/lib/gcc -type f -and \( -executable -o -name '*.o' \) -exec strip '{}' \;

  cd "$srcdir"/ghdl-gcc

  make DESTDIR="$pkgdir" install
}

package_python-pyghdl() {
  pkgdesc='Python bindings for GHDL'
  depends=('ghdl' 'python-pydecor' 'python-pyvhdlmodel')

  cd pyghdl

  python setup.py install --root="$pkgdir" --optimize=1 --skip-build
}
